﻿using System;
using System.Collections.Generic;

namespace DBCore.DB
{
    [Serializable()]
    public class SqlParam
    {
        public string Text;
        public List<DbParam> Parameters = new List<DbParam>();
        public SqlParam() { }
        public SqlParam(string text)
        {
            this.Text = text;
        }
        public SqlParam(string text, DbParam par)
        {
            this.Text = text;
            if (par != null)
            {
                this.Parameters.Add(par);
            }
        }
        public SqlParam(string text, List<DbParam> pars)
        {
            this.Text = text;
            if (pars != null)
            {
                this.Parameters = pars;
            }
        }
        public void AddParameter(DbParam par)
        {
            this.Parameters.Add(par);
        }

        public void OnlyAddParameter(SqlParam par)
        {
            if (par != null && par.Parameters != null)
            {
                this.Parameters.AddRange(par.Parameters);
            }
        }

        public void ParAnd(SqlParam par)
        {
            if (par != null)
            {
                if (!string.IsNullOrEmpty(this.Text))
                {
                    if (par.Text != null)
                    {
                        this.Text = "(" + this.Text + ") and (" + par.Text + ")";
                    }
                }
                else
                {
                    if (par.Text != null)
                    {
                        this.Text = par.Text;
                    }
                }
                if (this.Parameters == null)
                {
                    if (par.Parameters != null)
                    {
                        this.Parameters = par.Parameters;
                    }
                }
                else
                {
                    if (par.Parameters != null)
                    {
                        this.Parameters.AddRange(par.Parameters);
                    }
                }
            }
        }

        public void And(SqlParam par)
        {
            if (par == null || par.Text == null || par.Text == "") return;
            if (!string.IsNullOrEmpty(this.Text))
            {
                this.Text = "(" + this.Text + ") and (" + par.Text + ")";
                this.Parameters.AddRange(par.Parameters);
            }
            else
            {
                this.Text = par.Text;
                this.Parameters = par.Parameters;
            }
        }

        public SqlParam Join(SqlParam par)
        {
            var p = this.Clone();
            if (par != null && par.Text != null && par.Text != "")
            {
                if (!string.IsNullOrEmpty(this.Text))
                {
                    p.Text = "(" + this.Text + ") and (" + par.Text + ")";
                    p.Parameters.AddRange(par.Parameters);
                }
                else
                {
                    p.Text = par.Text;
                    p.Parameters = par.Parameters;
                }
            }
            return p;
        }

        public SqlParam Clone()
        {
            var p = new SqlParam();
            if (this.Text != null)
            {
                p.Text = this.Text;
            }
            if (this.Parameters != null)
            {
                foreach (var par in this.Parameters)
                {
                    p.Parameters.Add(par);
                }
            }
            return p;
        }

        public static implicit operator SqlParam(string text)
        {
            return new SqlParam(text);
        }
    }
}
